import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @description:callable创建线程
 * @author:Mr.Cat
 * @create:2021-09-25 09:40
 **/
public class TestCallable {
    public static void main(String[] args) {
        numThread n1=new numThread();
        FutureTask futureTask = new FutureTask(n1);
        Thread thread = new Thread(futureTask);
        thread.start();
        try {
            //get方法的返回值即为FutureTask构造器参数Callable实现类重写的call（）的返回值
            Object sum=futureTask.get();
            System.out.println("最总结果为："+sum);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}

class numThread implements Callable {
    @Override
    public Object call() throws Exception {
        int sum = 0;
        for (int i = 1; i <= 100; i++) {
            if (i % 2 == 0) {
                System.out.println(i);
                sum += i;
            }
        }
        return sum;
    }
}
